<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
  <style>
     .ani-enter{
      /* 定义初始状态 */
      transform: translateX(-100%);
      opacity: 0;
    }
    .ani-enter-active,.ani-leave-active{
      transition: all 500ms;
    }
    .ani-leave-to{
      transform: translateX(100%);
      opacity: 0;
    }
  </style>
</head>

<body>
  <div id="app">
    <div id="todos" style="max-width: 800px;margin:20px auto;border:1px solid #333;padding: 10px">
      <div class="row">
        <div class="col-md-10">
          <input type="text" class="form-control" v-model.trim="todo">
        </div>
        <div class="col-md-2">
          <button @click="addTodo" class="btn btn-success">增加待办事项</button>
        </div>
      </div>
      <!-- 待办事项列表 -->
      <ul class="list-group" style="margin-top: 30px">
        <li v-if="todos.length===0" class="list-group-item text-center">暂无待办事项</li>
        <transition-group name="ani">
          <li v-for="(todo,index) in todos" :key="index" class="list-group-item clearfix">
            <span>
              {{ todo.content }}
            </span>
            <div class="btns pull-right">
              <button 
                class="btn btn-primary btn-sm" 
                @click="setTodoState(index)"
                :disabled="todo.isFinished">
                {{todo.isFinished?'已完成':'点击完成'}}
              </button>
              <button 
                @click="delTodo(index)"
                class="btn btn-danger btn-sm">删除</button>
            </div>
          </li>
        </transition-group>
      </ul>
      <!-- /待办事项列表 -->
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        todo: '', //输入的待办事项
        todos: [
        ]
      },
      methods: {
        setTodoState(index){
          // 设置 已完成
          this.todos[index].isFinished = true;
        },
        delTodo(index){
          // 删除todo
          if(confirm('您确认删除嘛?')){
            this.todos.splice(index, 1);
          }
        },
        addTodo() {
          // 如果为空不添加
          if (this.todo === '') {
            return;
          }
          // 添加待办事项
          this.todos.push({
            content: this.todo,
            isFinished: false
          })
          this.todo = '';
        }
      }
    })
  </script>
</body>

</html>